<template>
  <div class="app-container">
    <el-card>
      <main-header @reset="reset" @search="search" @add="add" />
      <list
        @changeCurrentPage="changeCurrentPage"
        @changePagesize="changePagesize"
        @edit="edit"
        @setting="setting"
        @del="del"
      />
    </el-card>
    <el-dialog
      v-el-drag-dialog
      :title="title"
      :visible.sync="dialogVisible"
      width="60%"
    >
      <main-form
        :form-data="formData"
        @submit="submit"
      />
    </el-dialog>
    <el-dialog
      v-el-drag-dialog
      title="气象站定义"
      :visible.sync="weatherDialogVisible"
      width="60%"
    >
      <weather :form-data="formData" @submit="weatherSubmit" />
    </el-dialog>
    <el-dialog
      v-el-drag-dialog
      title="地块传感通道定义"
      :visible.sync="fieldSensorDialogVisible"
      width="60%"
    >
      <field-sensor :form-data="formData" @submit="fieldSensorSubmit" />
    </el-dialog>
    <add ref="add" :devices-obj="devicesObj" @addSubmit="addSubmit" />
  </div>
</template>

<script>
import elDragDialog from '@/directive/el-drag-dialog'
import MainHeader from './component/main-header'
import List from './component/list'
import MainForm from './component/main-form'
import Weather from './component/weather'
import FieldSensor from './component/field-sensor'
import Add from './component/add'
import CommonMixin from '@/mixin/CommonMixin'
import { getOne, updateOne, getIdataDevices, updateIdataDevices, deleteIdataDevice } from '@/api/device'
import { updateOne as updateWeatherDef } from '@/api/weatherDef'
import { removeEmptyField, clearCache } from '@/utils'
import { mapGetters } from 'vuex'
import { dclassCodeMap } from '@/utils/constant'
import { scadaId } from '@/utils/constant'
export default {
  components: {
    MainHeader,
    List,
    MainForm,
    Weather,
    FieldSensor,
    Add
  },
  directives: { elDragDialog },
  mixins: [CommonMixin],
  inject: ['reload'],
  data() {
    return {
      weatherDialogVisible: false,
      fieldSensorDialogVisible: false,
      devicesObj: {}
    }
  },
  computed: {
    ...mapGetters(['project', 'devices'])
  },
  created() {
    this.getIdataDevices()
  },
  methods: {
    async getIdataDevices() {
      const { data } = await getIdataDevices({ project_id: this.project.id, scada_id: scadaId })
      this.devicesObj = data
    },
    submit(formData) {
      formData = removeEmptyField(formData)
      this.updateOne(formData)
    },
    updateOne(formData) {
      updateOne(formData.serialno, formData).then(res => {
        this._afterSubmit(formData.serialno)
      })
    },
    getOne(row) {
      getOne(row.serialno).then(res => {
        this.formData = res.data
        this.dialogVisible = true
      })
    },
    _afterSubmit() {
      clearCache()
      this.$store.dispatch('device/getAll', { project_id: this.$store.getters.project.id }).then(res => {
        this.$message.success(res.msg)
      })
    },
    setting(row) {
      this.formData = row
      const product_type = +row.product_type
      switch (product_type) {
        case dclassCodeMap.qxz:
          this.weatherDialogVisible = true
          break
        case dclassCodeMap.sqz:
          this.formData.ports = []
          this.fieldSensorDialogVisible = true
          break
      }
    },
    async weatherSubmit(formData) {
      const { msg } = await updateWeatherDef(formData.serialno, formData)
      this.$message.success(msg)
      this.weatherDialogVisible = false
    },
    fieldSensorSubmit(formData) {
      this.$message.success('操作成功')
      this.fieldSensorDialogVisible = false
    },
    del(row) {
      this.$confirm('确定要删除该条记录吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          deleteIdataDevice(row.serialno).then((res) => {
            this.$message.success(res.msg)
            localStorage.removeItem('intelirri-devicesObj')
            this.$store.dispatch('device/getAll', { project_id: this.project.id }).then(res => {
              this.reload()
            })
          })
        })
        .catch(() => {
          console.log('cancel')
        })
    },
    add() {
      this.$refs.add.show()
    },
    async updateIdataDevices(serialnos) {
      const { msg } = await updateIdataDevices(this.project.id, serialnos)
      this.$message.success(msg)
      localStorage.removeItem('intelirri-devicesObj')
      this.$store.dispatch('device/getAll', { project_id: this.project.id }).then(res => {
        this.reload()
      })
    },
    addSubmit(formData) {
      this.updateIdataDevices(formData)
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
